package com.wy.service.impl;

import com.wy.service.FileService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

@Service
public class FileServiceImpl implements FileService {

    @Override
    public String handle(MultipartFile multipartFile) {
        String fileName = multipartFile.getName();
        String originalFilename = multipartFile.getOriginalFilename();
        long size = multipartFile.getSize();

        StringBuilder str = new StringBuilder();
        str.append("fileName:").append(fileName).append("  ,originalFilename:").append(originalFilename).append("  ,fileSize:").append(size);
        return str.toString();
    }

    @Override
    public void downloadToLocal(String fileName, javax.servlet.http.HttpServletResponse response) throws java.io.IOException {
        // 假设文件存储在 resources/upload 目录下
        String filePath = "D:\\gitee_versionControllerProject\\common-project-related\\09文件上传与下载\\code\\file-related-Commons FileUpload\\file.jpg";
        java.io.File file = new java.io.File(filePath);
        if (!file.exists()) {
            response.setStatus(404);
            response.getWriter().write("文件不存在");
            return;
        }
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition", "attachment;filename=" + java.net.URLEncoder.encode(fileName, "UTF-8"));
        java.io.FileInputStream fis = new java.io.FileInputStream(file);
        java.io.OutputStream os = response.getOutputStream();
        byte[] buffer = new byte[1024];
        int len;
        while ((len = fis.read(buffer)) != -1) {
            os.write(buffer, 0, len);
        }
        fis.close();
        os.flush();
        os.close();
    }

    @Override
    public void previewFile(String fileName, javax.servlet.http.HttpServletResponse response) throws java.io.IOException {
        // 假设文件存储在 resources/upload 目录下
        String filePath = "D:\\gitee_versionControllerProject\\common-project-related\\09文件上传与下载\\code\\file-related-Commons FileUpload\\file.jpg";
        java.io.File file = new java.io.File(filePath);
        if (!file.exists()) {
            response.setStatus(404);
            response.getWriter().write("文件不存在");
            return;
        }
        // 通过文件扩展名简单判断 contentType
        String contentType = null;
        String ext = fileName.substring(fileName.lastIndexOf('.') + 1).toLowerCase();
        switch (ext) {
            case "jpg": case "jpeg": contentType = "image/jpeg"; break;
            case "png": contentType = "image/png"; break;
            case "gif": contentType = "image/gif"; break;
            case "pdf": contentType = "application/pdf"; break;
            case "txt": contentType = "text/plain"; break;
            default: contentType = "application/octet-stream";
        }
        response.setContentType(contentType);
        response.setHeader("Content-Disposition", "inline;filename=" + java.net.URLEncoder.encode(fileName, "UTF-8"));
        java.io.FileInputStream fis = new java.io.FileInputStream(file);
        java.io.OutputStream os = response.getOutputStream();
        byte[] buffer = new byte[1024];
        int len;
        while ((len = fis.read(buffer)) != -1) {
            os.write(buffer, 0, len);
        }
        fis.close();
        os.flush();
        os.close();
    }
}
